//
//  FindPwdVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/19.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "FindPwdVC.h"
#import "AKBottomLineTextField.h"
#import "KKCountDownButton.h"
#import "YBApiManager+Login.h"
#import "NSString+Check.h"

@interface FindPwdVC ()
@property (weak, nonatomic) IBOutlet AKBottomLineTextField *mobileTF;
@property (weak, nonatomic) IBOutlet AKBottomLineTextField *pwdTF;
@property (weak, nonatomic) IBOutlet AKBottomLineTextField *repeatPwdTF;
@property (weak, nonatomic) IBOutlet AKBottomLineTextField *codeTF;
@property (nonatomic, strong) KKCountDownButton *countDownBtn;
@end

@implementation FindPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.mobileTF setLeftImageViewWithImageName:@"login_user"];
    [self.codeTF setLeftImageViewWithImageName:@"login_mobile"];
    [self.pwdTF setLeftImageViewWithImageName:@"login_pwd"];
    [self.repeatPwdTF setLeftImageViewWithImageName:@"login_pwd"];

    self.countDownBtn = [KKCountDownButton buttonWithType:UIButtonTypeCustom];
    self.countDownBtn.k_title(@"验证码");
    self.countDownBtn.count = 60;
    self.countDownBtn.frame = CGRectMake(0, 0, 60, 25);
    self.countDownBtn.k_font([UIFont systemFontOfSize:13]);
    [self.countDownBtn setTitleColor:UIColor.k_t1Color forState:UIControlStateNormal];
    //    self.countDownBtn.layer.cornerRadius = 3.f;
    self.codeTF.rightView = self.countDownBtn;
    self.codeTF.rightViewMode = UITextFieldViewModeAlways;
    [self.countDownBtn addTarget:self action:@selector(getCodeAction) forControlEvents:UIControlEventTouchUpInside];
    self.countDownBtn.layer.borderColor = UIColor.k_t1Color.CGColor;
    self.countDownBtn.layer.borderWidth = 0.5f;
    self.countDownBtn.layer.cornerRadius = 25/2;
    
}

- (void)getCodeAction {
    if ([NSString isPhoneNum:self.mobileTF.text] == NO) {
        [MBProgressHUD showTipMessageInView:@"请输入正确手机号"];
        return;
    }
    
    [MBProgressHUD showActivityMessageInView:@""];
    @weakify(self);
    [[[YBApiManager shareManager] getMobileCodeWithMobile:self.mobileTF.text event:@"resetpwd"] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInView:@"验证码发送成功"];
        @strongify(self);
        [self.countDownBtn startTimer];
        
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}

- (IBAction)findPwdAction:(id)sender {
    
    NSString *warning = nil;
    if ([NSString isPhoneNum:self.mobileTF.text] == NO) {
        warning = @"请输入正确的手机号";
    } else if (self.pwdTF.text.length < 6) {
        warning = @"请输入6位以上密码";
    } else if (self.codeTF.text.length < 4) {
        warning = @"请输入正确验证码";
    } else if ([self.pwdTF.text isEqualToString:self.repeatPwdTF.text] == NO) {
        warning = @"两次输入密码不一致";
    }
    if (warning) {
        [MBProgressHUD showTipMessageInView:warning];
        return;
    }
    
    [MBProgressHUD showActivityMessageInView:@""];
    [[YBApiManager.shareManager resetPwdWithMobile:self.mobileTF.text password:self.pwdTF.text repeatPwd:self.repeatPwdTF.text code:self.codeTF.text] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInWindow:@"操作成功" timer:1.0f];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}


- (IBAction)backBtnAction:(id)sender {
//    [self handleBackItemAction];
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
